function hfile = vmr_OverlaySRF(hfile, srf, ocolor)
% VMR::OverlaySRF  - overlay an SRF into the VMR (data changed!)
%
% FORMAT:       [vmr] = vmr.OverlaySRF(srf [, ocolor])
%
% Input fields:
%
%       srf         SRF object
%       ocolor      optional overlay color (default: 10, max positive LUT)
%
% Output fields
%
%       vmr         altered (!) object
%
% Note: since the overlay is done with the statistical LUT colors
%       the VMR object is changed !!! So, it does not work on V16 objects

% Version:  v0.7b
% Build:    7090216
% Date:     Sep-02 2007, 4:33 PM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin < 2 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, 'vmr') || ...
    numel(srf) ~= 1 || ...
   ~isBVQXfile(srf, 'srf')
    error( ...
        'BVQXfile:BadArguments', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end
bc = bvqxfile_getcont(hfile.L);
srfbc = bvqxfile_getcont(srf.L);
if ~bc.VMR8bit
    error( ...
        'BVQXfile:InvalidObject', ...
        'Only valid for 8-bit VMR objects.' ...
    );
end
if nargin < 3 || ...
   ~isnumeric(ocolor) || ...
    numel(ocolor) ~= 1
    ocolor = 10;
else
    ocolor = round(real(double(ocolor)));
end
if isinf(ocolor) || ...
    isnan(ocolor) || ...
    ocolor < 0 || ...
    ocolor > 30
    ocolor = 10;
end
ocolor = uint8(ocolor + 225);

% get good coordinates
c = round(srfbc.VertexCoordinate);
vs = size(bc.VMRData);
c(( ...
    c(:, 1) < 1 | c(:, 1) > vs(1) | ...
    c(:, 2) < 1 | c(:, 2) > vs(2) | ...
    c(:, 3) < 1 | c(:, 3) > vs(3)), :) = [];
c = sub2ind(vs, c(:, 1), c(:, 2), c(:, 3));

% set in VMRData
bc.VMRData(c) = ocolor;
bvqxfile_setcont(hfile.L, bc);
